package com.longe.poi;

import org.apache.poi.ss.usermodel.*;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

import java.io.File;
import java.io.FileOutputStream;
import java.util.Calendar;
import java.util.Date;

/**
 * 简单的对日期进行单元格格式处理
 */
public class SecondSimpleDemo {

    public static void main(String[] args) throws Exception {

        Workbook workbook = new XSSFWorkbook();
        Sheet sheet = workbook.createSheet("firstSheet");

        Row row = sheet.createRow(0);//第一行

        Cell cell = row.createCell(0);
        cell.setCellValue(new Date());//设置Date类型的

        /** 设置日期类单元格格式 **/
        CellStyle cellStyle = workbook.createCellStyle();
        CreationHelper creationHelper = workbook.getCreationHelper();
        cellStyle.setDataFormat(creationHelper.createDataFormat().getFormat("yyyy-MM-dd hh:mm:ss"));

        cell = row.createCell(1);
        cell.setCellValue(new Date());//设置Date类型的
        cell.setCellStyle(cellStyle);

        cell = row.createCell(2);
        cell.setCellValue(Calendar.getInstance());//设置Calendar类型的
        cell.setCellStyle(cellStyle);

        FileOutputStream fos = new FileOutputStream(new File("D:\\TEST_POI\\second.xlsx"));
        workbook.write(fos);
        fos.close();
        workbook.close();
    }

}
